<?php
namespace app\api\controller;

use app\api\Base;
use think\Config;
use think\Db;
use baseutils\BaseUtil; # 验证类
use com\gdl\newagk\interfaces\AdminCompanyServiceClient;
use com\gdl\newagk\dataobject\admincompany\CompanyDTO; 

/**
 * admin后台公司相关接口 包含公司 部门 班组 专业
 * @author tc 2018/04/27
 */

class Admincompany extends Base
{
 	/** 
 	 *  公司列表   
     *  @param  admin_reg or plaformId
 	 *  @return JSON [description] 400失败 200成功
 	 */
	public function companyList() 
    {
    	$msg = ['code'=>400, 'msg'=>'暂无数据', 'data'=>[],'count'=>0];
        $plaformId = $this->request->param('plaformId') ? $this->request->param('plaformId') : 0; # 平台id
        $liekName = $this->request->param('name') ? $this->request->param('name') : ""; #公司名称
        $plaformChildId = $this->request->param('plaformChildId') ? $this->request->param('plaformChildId') : 0; # 赛选平台
        $status = $this->request->param('status')?$this->request->param('status'):""; # 1：启用 2：禁用 3：平台禁用
        $offset = $this->request->param('offset') ? $this->request->param('offset') : 0; # 分页
        $num = $this->request->param('num') ? $this->request->param('num') : 10; # 条数
        $reg = $this->request->param('reg')?$this->request->param('reg'):"";
        if(empty($reg)){
            $msg['msg'] = "请先登录";
            return json_encode($msg);
        }

        # 获取该平台下子平台和当前平台的数据
         $plaformId = $plaformChildId ? $plaformChildId : $plaformId;
        if($plaformId==0){
            $adminReg = getApiAdminReg($plaformId,'all');     
        }else{
            $adminReg = getApiAdminReg($plaformId,'child'); 
        } 
        try { 
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
            $AdminCompanyService = new AdminCompanyServiceClient(NULL);
            \ApiClient::build($AdminCompanyService);
            $CompanyDo = new CompanyDTO(); 
            
            $CompanyDo->offset = $offset;
            $CompanyDo->num = $num;   
            
            $where = " isdelete = 0 and admin_reg in ('{$adminReg}') ";
            # 模糊查询
            if($liekName){
                $where .= " and (name like '%".$liekName."%' or id='".$liekName."' or remark like '%".$liekName."%') ";
            }
            if($status==1){
                $where.=" and status=1 ";   
            }
            if($status==2){
                $where.=" and status=0 ";   
            }
            if($status==3){
                $where.=" and status=2 ";   
            }
            $CompanyDo->where = $where; 

            $result = $AdminCompanyService->companyList($CompanyDo); 
            if ($result->success == TRUE) {
            	$msg['msg'] = "获取成功";
                $msg['code'] = 200;
                $msg['data'] = $result->data; 
                $msg['count'] = intval($result->message);
            } else {
                SLogger::getLogger('pub_thrift_service', 'thrift_error')->error('error', $result->message);
            }

        } catch (Exception $ex) {
            SLogger::getLogger('pub_thrift_service', 'thrift_error')->error('thrift call error', $ex);
        }
        return json_encode($msg);
    }
    /**
     * 查询公司信息
     */
    public function companyInfo(){
        $msg = ['code'=>400, 'msg'=>'获取失败', 'data'=>[]];
        $id = $this->request->param("id") ? $this->request->param("id"): ""; 
        if(empty($id)){
            $msg['msg'] = "缺少公司标识";
            return json_encode($msg);
        }
        try{
            $ret = Db::name('company')->where(['id'=>$id,'isdelete'=>0])->field("id,name,remark,status,admin_reg")->find();
            $p_wheres['admin_reg'] = $ret['admin_reg'];
            $plaforms_id = Db::name("plaform")->where($p_wheres)->field("id,reals,adminRealname,admin_reg")->find();
            $ret['plaformid'] = $plaforms_id['id']?$plaforms_id['id']:0;
            if($ret){
                $msg['code'] = 200;
                $msg['msg'] = "获取成功";
                $msg['data'] = $ret;
            }
        }catch(Exception $ex){
            $msg['msg'] = $ex->getMessage();
        }
        return json_encode($msg);
    }
    /**
     * 获取平台列表
     */
    public function getPlaformList(){
        $msg = ['code'=>400, 'msg'=>"暂无平台", 'data'=>[]];
        $plaformId = $this->request->param("plaformId") ?$this->request->param("plaformId") :0;
        $admin_reg = $this->request->param("admin_reg") ?$this->request->param("admin_reg"):"";

        try{
            $where['isdelete'] =0;
            $where['status']  = 1;
            if($plaformId!=0){
                $where['id'] = $plaformId;
                $where['parentid'] = $plaformId;
            }
            $plaformList = Db::name("plaform")->where($where)->field("id,reals,adminRealname,admin_reg")->select();
            if($plaformList){
                $msg['msg'] = '获取成功';
                $msg['code'] = 200;
                $msg['data'] = $plaformList;
            }
            
        }catch(Exception $e){
            $msg['msg'] = $e->getMessage();
        }
        return json_encode($msg);
    }
    /**
     * 获取公司列表
     */
    // public function getCompanyList(){
    //     $plaformId = $this->request->param("plaform_id") ? $this->request->param("plaform_id") : "";
    //     $admin_reg = $this->request->param("admin_reg") ? $this->request->param("admin_reg"): "";
    //     if(empty($plaformId) || empty($admin_reg)){
    //         $msg['msg'] = "缺少必要参数";
    //         return json_encode($msg);
    //     }
        
    //     try{
            
    //     }catch(Exception $e){
    //         $msg['msg'] = $e->getMessage();
    //     }
    //     return json_encode($msg);
    // }
    /**
     * 添加修改公司
     */
    public function editCompany(){
        $msg = ['code'=>400, 'msg'=>'暂无数据', 'data'=>[]];
        $plaformId = $this->request->param('plaformId') ? $this->request->param('plaformId') : 0; # 平台id
        $admin_reg = $this->request->param('reg') ? $this->request->param('reg'):"";
        $data = $this->request->post();
        if(!$data){
            $msg['msg'] = "请带入相关参数";
        }
        $datas = [
            'id'=>isset($data['id'])?$data['id']:"",
            'name'=>isset($data['name'])?$data['name']:"",
            'pid'=>isset($data['pid'])?$data['pid']:0,
            'remark'=>isset($data['remark'])?$data['remark']:"0",
            'createTime'=>time(),
            'updateTime'=>time(),
            'isdelete'=>0,
            'status'=>isset($data['status'])?$data['status']:1,
            'projectId'=>isset($data['project_id'])?$data['project_id']:0,
            'admin_id'=>1,
            'intendant'=>isset($data['intendant'])?$data['intendant']:0,
        ];
        if(empty($datas['name'])){
            $msg['msg'] = "请输入名称";
            return json_encode($msg);
        }
        $qbankName = format_html($data['name']);
        if($qbankName){
            $wheres['name'] = $qbankName;
            if($data['id']){
                $wheres['id'] = ['neq',$data['id']];
            }
            $name = Db::name('company')->where($wheres)->field("id")->find();
            if($name){
                $msg['msg'] = "已存在该公司，请重新修改";
                return json_encode($msg);
            }
        }
        try{

            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
            $AdminCompanyService = new AdminCompanyServiceClient(NULL);
            \ApiClient::build($AdminCompanyService);
            $CompanyDo = new CompanyDTO(); 
            $CompanyDo->id = $plaformId;
            $CompanyDo->data = $datas;
            $result = $AdminCompanyService->editCompany($CompanyDo);
            if ($result->success == TRUE) {
                $msg['msg'] = $result->message;
                $msg['code'] = $result->code;
                $msg['data'] = $result->data; 
            } 
        }catch(Exception $e){
            SLogger::getLogger('pub_thrift_service', 'thrift_error')->error('thrift call error', $ex);
        }

        return json_encode($msg);
    }

    /** 
     *  部门列表   
     *  @param  admin_reg or plaformId
     *  @return JSON [description] 400失败 200成功
     */
    public function departmentList() 
    {
        $msg = ['code'=>400, 'msg'=>'暂无数据', 'data'=>[],'count'=>0];
        $plaformId = $this->request->param('plaformId') ? $this->request->param('plaformId') : 0; # 平台id
        $liekName = $this->request->param('name') ? $this->request->param('name') : ""; #部门名称
        $companyId = $this->request->param('companyid') ? $this->request->param('companyid') : ""; #公司id
        $plaformChildId = $this->request->param('plaformChildId') ? $this->request->param('plaformChildId') : 0; # 赛选平台
        $offset = $this->request->param('offset') ? $this->request->param('offset') : 0; # 分页
        $num = $this->request->param('num') ? $this->request->param('num') : 10; # 条数

        $status = $this->request->param('status')?$this->request->param('status'):""; # 1：启用 2：禁用 3：平台禁用

        $reg = $this->request->param('reg')?$this->request->param('reg'):"";
        if(empty($reg)){
            $msg['msg'] = "请先登录";
            return json_encode($msg);
        }
        # 获取该平台下子平台和当前平台的数据
         $plaformId = $plaformChildId ? $plaformChildId : $plaformId;
        if($plaformId==0){
            $adminReg = getApiAdminReg($plaformId,'all');     
        }else{
            $adminReg = getApiAdminReg($plaformId,'child'); 
        } 
        
        try { 
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
            $AdminCompanyService = new AdminCompanyServiceClient(NULL);
            \ApiClient::build($AdminCompanyService);
            $CompanyDo = new CompanyDTO(); 
            
            $CompanyDo->offset = $offset;
            $CompanyDo->num = $num; 
           
            $where = " isdelete = 0 and pid = 0 and admin_reg in ('{$adminReg}') ";
            # 模糊查询
            if($liekName){
                $where .= " and (name like '%".$liekName."%' or id='".$liekName."' or remark like '%".$liekName."%') ";
                // $where .= " and name like '%".$liekName."%' ";
            }
            # 公司id
            if($companyId){
                $where .= " and companyId ='{$companyId}'";
            }
            if($status==1){
                $where.=" and status=1 ";   
            }
            if($status==2){
                $where.=" and status=0 ";   
            }
            if($status==3){
                $where.=" and status=2 ";   
            }
            $CompanyDo->where = $where;  
            $result = $AdminCompanyService->departmentList($CompanyDo); 

            if ($result->success == TRUE) {
                $msg['msg'] = "获取成功";
                $msg['code'] = $result->code;
                if($result->data){
                    $plaformList = Db::name('plaform')->where(['isdelete'=>0])->field('id,reals,adminRealname,admin_reg')->select();
                    for($i=0; $i<count($result->data); $i++){
                        for($j=0; $j<count($plaformList);$j++){
                            if($result->data[$i]['admin_reg']==$plaformList[$j]['admin_reg']){
                                $result->data[$i]['plaformname'] = $plaformList[$j]['reals'];
                            }
                        }
                    }
                }
                $msg['data'] = $result->data; 
                $msg['count'] = intval($result->message);
            } else {
                SLogger::getLogger('pub_thrift_service', 'thrift_error')->error('error', $result->message);
            }

        } catch (Exception $ex) {
            SLogger::getLogger('pub_thrift_service', 'thrift_error')->error('thrift call error', $ex);
        }
        return json_encode($msg);
    }

    /**
     * 添加修改部门
     */
    public function editDepartment(){
        $msg = ['code'=>400, 'msg'=>'暂无数据', 'data'=>[]];
        $plaformId = $this->request->param('plaformId') ? $this->request->param('plaformId') : 0; # 平台id
        $data = $this->request->post();
        if(!$data){
            $msg['msg'] = "请带入相关参数";
        }
        $datas = [
            'id'=>isset($data['id'])?$data['id']:0,
            'name'=>isset($data['name'])?$data['name']:"",
            'pid'=>isset($data['pid'])?$data['pid']:0,
            'remark'=>isset($data['remark'])?$data['remark']:"0",
            'createTime'=>time(),
            'updateTime'=>time(),
            'isdelete'=>0,
            'status'=>isset($data['status'])?$data['status']:1,
            'companyId'=>isset($data['company_id'])?$data['company_id']:0,
            'admin_id'=>1,
        ];
            if(empty($datas['name'])){
                        $msg['msg'] = "请输入名称";
                        return json_encode($msg);
            }

        $name = format_html($data['name']);
        if($name){
            $wheres['name'] = $name;
            if($data['id']){
                $wheres['id'] = ['neq',$data['id']];
            }
            if($data['company_id']){
                $wheres['companyId'] =  $data['company_id']; 
            }
            $extName = Db::name('department')->where($wheres)->field("id")->find();
            if($extName){
                $msg['msg'] = "已存在，请勿重复添加";
                return json_encode($msg);
            }
        }
        try{

            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
            $AdminCompanyService = new AdminCompanyServiceClient(NULL);
            \ApiClient::build($AdminCompanyService);
            $CompanyDo = new CompanyDTO(); 
            $CompanyDo->id = $plaformId;
            $CompanyDo->data = $datas;
            $result = $AdminCompanyService->editDepartment($CompanyDo);
            if ($result->success == TRUE) {
                $msg['msg'] = $result->message;
                $msg['code'] = $result->code;
                $msg['data'] = $result->data; 
            } 
        }catch(Exception $e){
            SLogger::getLogger('pub_thrift_service', 'thrift_error')->error('thrift call error', $e);
        }

        return json_encode($msg);
    }
    /**
     * 查询部门信息
     */
    public function departmentInfo(){
        $msg = ['code'=>400, 'msg'=>'获取失败', 'data'=>[]];
        $id = $this->request->param("id") ? $this->request->param("id"): ""; 
        if(empty($id)){
            $msg['msg'] = "请选择需要修改的部门";
            return json_encode($msg);
        }
        try{
            $ret = Db::name('department')->where(['id'=>$id,'isdelete'=>0])->field("id,name,remark,status,companyId,admin_reg")->find();
            $p_wheres['admin_reg'] = $ret['admin_reg'];
            $plaforms_id = Db::name("plaform")->where($p_wheres)->field("id,reals,adminRealname,admin_reg")->find();
            $ret['plaformid'] = $plaforms_id['id']?$plaforms_id['id']:0;
            if($ret){
                $msg['code'] = 200;
                $msg['msg'] = "获取成功";
                $msg['data'] = $ret;
            }
        }catch(Exception $ex){
            $msg['msg'] = $ex->getMessage();
        }
        return json_encode($msg);
    }
    /** 
     *  班组列表   
     *  @param  admin_reg or plaformId
     *  @return JSON [description] 400失败 200成功
     */
    public function groupList() 
    {
        $msg = ['code'=>400, 'msg'=>'暂无数据', 'data'=>[],'count'=>0];
        $plaformId = $this->request->param('plaformId') ? $this->request->param('plaformId') : 0; # 平台id
        $liekName = $this->request->param('name') ? $this->request->param('name') : ""; #部门名称
        $companyId = $this->request->param('companyid') ? $this->request->param('companyid') : ""; #公司id
        $departmentId = $this->request->param('departmentid') ? $this->request->param('departmentid') : ""; #部门id
        $plaformChildId = $this->request->param('plaformChildId') ? $this->request->param('plaformChildId') : 0; # 赛选平台
        $offset = $this->request->param('offset') ? $this->request->param('offset') : 0; # 分页
        $num = $this->request->param('num') ? $this->request->param('num') : 10; # 条数

        $status = $this->request->param('status')?$this->request->param('status'):""; # 1：启用 2：禁用 3：平台禁用
        $reg = $this->request->param('reg')?$this->request->param('reg'):"";
        if(empty($reg)){
            $msg['msg'] = "请先登录";
            return json_encode($msg);
        }
        # 获取该平台下子平台和当前平台的数据
         $plaformId = $plaformChildId ? $plaformChildId : $plaformId;
        if($plaformId==0){
            $adminReg = getApiAdminReg($plaformId,'all');     
        }else{
            $adminReg = getApiAdminReg($plaformId,'child'); 
        } 
        
        try { 
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
            $AdminCompanyService = new AdminCompanyServiceClient(NULL);
            \ApiClient::build($AdminCompanyService);
            $CompanyDo = new CompanyDTO(); 
            
            $CompanyDo->offset = $offset;
            $CompanyDo->num = $num;   
            $where = " isdelete = 0 and pid <> 0 and admin_reg in ('{$adminReg}') ";
            # 模糊查询
            if($liekName){
                // $where .= " and name like '%".$liekName."%' ";
                // $where .= " and (name like '%".$liekName."%' or id=".$liekName." or remark like '%".$liekName."%')";
                $where .= " and (name like '%".$liekName."%' or id='".$liekName."' or remark like '%".$liekName."%')";
            }
            # 公司id
            if($companyId){
                $where .= " and companyId ='{$companyId}'";
            }
            # 部门id
            if($departmentId){
                $where .= " and pid='{$departmentId}' ";
            }
            if($status==1){
                $where.=" and status=1 ";   
            }
            if($status==2){
                $where.=" and status=0 ";   
            }
            if($status==3){
                $where.=" and status=2 ";   
            }
            $CompanyDo->where = $where;  
            $result = $AdminCompanyService->groupList($CompanyDo); 
            if ($result->success == TRUE) {
                $msg['msg'] = "获取成功";
                $msg['code'] = $result->code;
                if($result->data){
                    $maps['isdelete'] = 0;
                    $maps['pid'] = 0;
                    $departmentlist = Db::name('department')->where($maps)->field('id,pid,name,admin_reg')->select(); 
                    $plaformList = Db::name('plaform')->where(['isdelete'=>0])->field('id,reals,adminRealname,admin_reg')->select(); 
                    for($i=0; $i<count($result->data); $i++){
                        for($j=0; $j<count($departmentlist);$j++){
                            if($result->data[$i]['pid']==$departmentlist[$j]['id']){
                                $result->data[$i]['departmentname'] = $departmentlist[$j]['name'];
                            }
                        }
                        for($j=0; $j<count($plaformList); $j++){ 
                            if($result->data[$i]['admin_reg']==$plaformList[$j]['admin_reg']){
                                $result->data[$i]['plaformname'] = $plaformList[$j]['reals'];

                            }
                        }
                    }
                }
                $msg['data'] = $result->data; 
                $msg['count'] = intval($result->message);
            } else {
                SLogger::getLogger('pub_thrift_service', 'thrift_error')->error('error', $result->message);
            }

        } catch (Exception $ex) {
            SLogger::getLogger('pub_thrift_service', 'thrift_error')->error('thrift call error', $ex);
        }
        return json_encode($msg);
    }

    /**
     * 添加修改班组
     */
    public function editGroup(){
        $msg = ['code'=>400, 'msg'=>'暂无数据', 'data'=>[]];
        $plaformId = $this->request->param('plaformId') ? $this->request->param('plaformId') : 0; # 平台id
        $data = $this->request->post();
        if(!$data){
            $msg['msg'] = "请带入相关参数";
        }
        $datas = [
            'id'=>isset($data['id'])?$data['id']:"",
            'name'=>isset($data['name'])?$data['name']:"",
            'pid'=>isset($data['pid'])?$data['pid']:0,
            'remark'=>isset($data['remark'])?$data['remark']:"0",
            'createTime'=>time(),
            'updateTime'=>time(),
            'isdelete'=>0,
            'status'=>isset($data['status'])?$data['status']:1,
            'companyId'=>isset($data['company_id'])?$data['company_id']:0,
            'admin_id'=>1,
        ];
        // var_dump($datas);exit;
        if(empty($datas['name'])){
            $msg['msg'] = "请输入名称";
            return json_encode($msg);
        }
        $name = format_html($data['name']);
        if($name){
            $wheres['name'] = $name;
            if($data['id']){
                $wheres['id'] = ['neq',$data['id']];
            }
            if($data['pid']){
                $wheres['pid'] =  $data['pid']; 
            }
            $extName = Db::name('department')->where($wheres)->field("id")->find();
            if($extName){
                $msg['msg'] = "已存在该班组，请勿重复添加";
                return json_encode($msg);
            }
        }
        try{

            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
            $AdminCompanyService = new AdminCompanyServiceClient(NULL);
            \ApiClient::build($AdminCompanyService);
            $CompanyDo = new CompanyDTO(); 
            $CompanyDo->id = $plaformId;
            $CompanyDo->data = $datas;
            $result = $AdminCompanyService->editGroup($CompanyDo);
            if ($result->success == TRUE) {
                $msg['msg'] = $result->message;
                $msg['code'] = $result->code;
                $msg['data'] = $result->data; 
            } 
        }catch(Exception $e){
            SLogger::getLogger('pub_thrift_service', 'thrift_error')->error('thrift call error', $e);
        }

        return json_encode($msg);
    }
    /**
     * 查询班组信息
     */
    public function groupInfo(){
        $msg = ['code'=>400, 'msg'=>'获取失败', 'data'=>[]];
        $id = $this->request->param("id") ? $this->request->param("id"): ""; 
        if(empty($id)){
            $msg['msg'] = "请选择需要修改的班组";
            return json_encode($msg);
        }
        try{
            $ret = Db::name('department')->where(['id'=>$id,'isdelete'=>0])->field("id,name,remark,status,companyId,pid as department,admin_reg")->find();
            $p_wheres['admin_reg'] = $ret['admin_reg'];
            $plaforms_id = Db::name("plaform")->where($p_wheres)->field("id,reals,adminRealname,admin_reg")->find();
            $ret['plaformid'] = $plaforms_id['id']?$plaforms_id['id']:0;
            if($ret){
                $msg['code'] = 200;
                $msg['msg'] = "获取成功";
                $msg['data'] = $ret;
            }
        }catch(Exception $ex){
            $msg['msg'] = $ex->getMessage();
        }
        return json_encode($msg);
    }
    /** 
     *  专业列表
     *  @param  admin_reg or plaformId
     *  @return JSON [description] 400失败 200成功
     */
    public function specialtyList() 
    {
        $msg = ['code'=>400, 'msg'=>'暂无数据', 'data'=>[],'count'=>0];
        $plaformId = $this->request->param('plaformId') ? $this->request->param('plaformId') : 0; # 平台id
        $liekName = $this->request->param('name') ? $this->request->param('name') : ""; #专业名称
        $plaformChildId = $this->request->param('plaformChildId') ? $this->request->param('plaformChildId') : 0; # 赛选平台
        $type = $this->request->param('type') ? $this->request->param('type') : 0; # 0专业 1职位
        $companyId = $this->request->param('companyId') ? $this->request->param('companyId') : 0; # 赛选公司
        $offset = $this->request->param('offset') ? $this->request->param('offset') : 0; # 分页
        $num = $this->request->param('num') ? $this->request->param('num') : 10; # 条数

        $status = $this->request->param('status') ? $this->request->param('status'):"";
        $reg = $this->request->param('reg')?$this->request->param('reg'):"";
        if(empty($reg)){
            $msg['msg'] = "请先登录";
            return json_encode($msg);
        }
       # 获取该平台下子平台和当前平台的数据
         $plaformId = $plaformChildId ? $plaformChildId : $plaformId;
        if($plaformId==0){
            $adminReg = getApiAdminReg($plaformId,'all');     
        }else{
            $adminReg = getApiAdminReg($plaformId,'child'); 
        } 
        
        try { 
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
            $AdminCompanyService = new AdminCompanyServiceClient(NULL);
            \ApiClient::build($AdminCompanyService);
            $CompanyDo = new CompanyDTO(); 
            
            $CompanyDo->offset = $offset;
            $CompanyDo->num = $num;   
            $where = " isdelete = 0 and type='{$type}' and admin_reg in ('{$adminReg}') ";
            # 模糊查询
            if($liekName){
                $where .= " and (name like '%".$liekName."%' or id='".$liekName."' or remark like '%".$liekName."%')";
            }  
            # 查询属于哪个公司
            if($companyId){
                $where .= " and companyId = ".$companyId;
            }
            if($status==1){
                $where.=" and status=1 ";   
            }
            if($status==2){
                $where.=" and status=0 ";   
            }
            if($status==3){
                $where.=" and status=2 ";   
            }
            $CompanyDo->where = $where;  
            $result = $AdminCompanyService->specialtyList($CompanyDo);
            if ($result->success == TRUE) {

                $msg['msg'] = "获取成功";
                $plaformList = Db::name('plaform')->where(['isdelete'=>0])->field('id,reals,adminRealname,admin_reg')->select(); 
                $filed = "id,name,status,admin_reg";
                $maps['isdelete'] = 0; 
                $getCompanyList = Db::name('company')->where($maps)->field($filed)->select();
                 for($i=0; $i<count($result->data); $i++ ){
                    $result->data[$i]['plaformname'] = "总平台"; 
                    $result->data[$i]['companyname'] = "暂无公司"; 
                    for($j=0; $j<count($plaformList); $j++){ 
                        if($result->data[$i]['admin_reg']==$plaformList[$j]['admin_reg']){
                            $result->data[$i]['plaformname'] = $plaformList[$j]['reals'];

                        }
                    }
                    for($k=0; $k<count($getCompanyList); $k++){
                        if($getCompanyList[$k]['id']==$result->data[$i]['companyId']){
                            $result->data[$i]['companyname'] = $getCompanyList[$k]['name'];
                        }
                    } 
                } 
                $msg['code'] = $result->code;
                $msg['data'] = $result->data; 

                $msg['count'] = intval($result->message);
            } else {
                SLogger::getLogger('pub_thrift_service', 'thrift_error')->error('error', $result->message);
            }

        } catch (Exception $ex) {
            SLogger::getLogger('pub_thrift_service', 'thrift_error')->error('thrift call error', $ex);
        }
        return json_encode($msg);
    }

     /**
     * 添加修改职位
     */
    public function editSpecialty(){
        $msg = ['code'=>400, 'msg'=>'暂无数据', 'data'=>[]];
        $plaformId = $this->request->param('plaformId') ? $this->request->param('plaformId') : 0; # 平台id
        $data = $this->request->post();
        if(!$data){
            $msg['msg'] = "请带入相关参数";
        }
        $datas = [
            'id'=>isset($data['id'])?$data['id']:0,
            'name'=>isset($data['name'])?$data['name']:"",
            'pid'=>isset($data['pid'])?$data['pid']:0,
            'remark'=>isset($data['remark'])?$data['remark']:"0",
            'type'=>isset($data['type'])?$data['type']:"0",# 0:专业 1：职位
            'companyId'=>isset($data['company_id'])?$data['company_id']:"0",# 所属公司
            'createTime'=>time(),
            'updateTime'=>time(),
            'isdelete'=>0,
            'status'=>isset($data['status'])?$data['status']:0,
            'admin_id'=>1,
        ];
        if(empty($datas['name'])){
            $msg['msg'] = "请输入名称";
            return json_encode($msg);
        }
        $name = format_html($data['name']);
        if($name){
            $wheres['name'] = $name;
            if($data['id']){
                $wheres['id'] = ['neq',$data['id']];
            }
            if($data['company_id']){
                $wheres['companyId'] =  $data['company_id']; 
            }
            $extName = Db::name('user_specialty')->where($wheres)->field("id")->find();
            if($extName){
                $msg['msg'] = "已存在该名称，请勿重复添加";
                return json_encode($msg);
            }
        }
        try{

            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
            $AdminCompanyService = new AdminCompanyServiceClient(NULL);
            \ApiClient::build($AdminCompanyService);
            $CompanyDo = new CompanyDTO(); 
            $CompanyDo->id = $plaformId;
            $CompanyDo->data = $datas;
            $result = $AdminCompanyService->editSpecialty($CompanyDo);
            if ($result->success == TRUE) {
                $msg['msg'] = $result->message;
                $msg['code'] = $result->code;
                $msg['data'] = $result->data; 
            } 
        }catch(Exception $e){
            SLogger::getLogger('pub_thrift_service', 'thrift_error')->error('thrift call error', $e);
        }

        return json_encode($msg);
    }
    /**
     * 查询专业职位信息
     */
    public function specialtyInfo(){
        $msg = ['code'=>400, 'msg'=>'获取失败', 'data'=>[]];
        $id = $this->request->param("id") ? $this->request->param("id"): ""; 
        if(empty($id)){
            $msg['msg'] = "请选择需要修改的数据";
            return json_encode($msg);
        }
        try{
            $ret = Db::name('user_specialty')->where(['id'=>$id,'isdelete'=>0])->field("id,name,remark,status,companyId,admin_reg")->find();
            $p_wheres['admin_reg'] = $ret['admin_reg'];
            $plaforms_id = Db::name("plaform")->where($p_wheres)->field("id,reals,adminRealname,admin_reg")->find();
            $ret['plaformid'] = $plaforms_id['id']?$plaforms_id['id']:0;
            if($ret){
                $msg['code'] = 200;
                $msg['msg'] = "获取成功";
                $msg['data'] = $ret;
            }
        }catch(Exception $ex){
            $msg['msg'] = $ex->getMessage();
        }
        return json_encode($msg);
    } 
     /**
     * 修改公司状态 
     * status 0：禁用 1:启用  2：平台禁用
     */
    public function editCompanyStatus(){
        $msg = ['code'=>400, 'msg'=>"修改失败", 'data'=>[]];
        $plaformId = $this->request->param('plaformId') ? $this->request->param('plaformId') : 0;
        $admin_reg = $this->request->param('reg') ? $this->request->param('reg') : "";
        $companyId = $this->request->param('companyId') ? $this->request->param('companyId') : "";
        $status = $this->request->param('status') ? $this->request->param('status') : "0";
       
        if(empty($companyId)){
            $msg['msg'] = "缺少必要参数";
            return json_encode($msg);
        } 
        try{ 
            $admin = Db::name("admin_user")->where(['id'=>1])->field('reg')->find();
            $where['id'] = ['in',$companyId];
            if($plaformId==0){ 
                    if($status==0){ # 禁用
                        $upstatus = Db::name('company')->where($where)->setField(['status'=>2]);
                    }else{ # 启用
                        $upstatus = Db::name('company')->where($where)->setField(['status'=>1]);
                    } 

            # 子平台修改状态
            }else{
                $wheres['id'] = ['in',$companyId];
                $upstatus = Db::name('company')->where($wheres)->setField(['status'=>$status]);
            }
            if($upstatus){
                $msg['code'] = 200;
                $msg['msg'] = "禁用成功";
                if($status==1){
                    $msg['msg'] = "启用成功";
                }
                
            }
        }catch(Exception $e){
            $msg['msg'] = $e->getMessage();
        }
        return json_encode($msg);
    }
     /**
     * 删除公司 
     */
    public function deleteCompanyStatus(){
        $msg = ['code'=>400, 'msg'=>"删除失败", 'data'=>[]];
        $plaformId = $this->request->param('plaformId') ? $this->request->param('plaformId') : 0;
        $admin_reg = $this->request->param('reg') ? $this->request->param('reg') : "";
        $companyId = $this->request->param('companyId') ? $this->request->param('companyId') : "";
        $delete = $this->request->param('delete') ? $this->request->param('delete') : "1";
       
        if(empty($companyId) || empty($delete)){
            $msg['msg'] = "缺少必要参数";
            return json_encode($msg);
        } 
        try{ 
            $where['id'] = ['in',$companyId];
            $upstatus = Db::name('company')->where($where)->setField(['isdelete'=>$delete]); 
            if($upstatus){
                $msg['code'] = 200;
                $msg['msg'] = "已删除";
            }
        }catch(Exception $e){
            $msg['msg'] = $e->getMessage();
        }
        return json_encode($msg);
    }
    
    /**
     * 导出公司列表
     */
    public function downCompanyExecl(){
        ob_end_clean(); # 清空缓冲区
        $msg = ['code'=>400, 'msg'=>'导出失败', 'data'=>[],'count'=>0];
        $plaformId = $this->request->param('plaformId') ? $this->request->param('plaformId') : 0; # 平台id
        $liekName = $this->request->param('name') ? $this->request->param('name') : ""; #公司名称
        $plaformChildId = $this->request->param('plaformChildId') ? $this->request->param('plaformChildId') : 0; # 赛选平台
        $offset = $this->request->param('offset') ? $this->request->param('offset') : 0; # 分页
        $num = $this->request->param('num') ? $this->request->param('num') : 10000; # 条数

        #选中导出
        $selectId = $this->request->param("selectId") ? $this->request->param("selectId"):""; # 选中需要导出的参数

       # 获取该平台下子平台和当前平台的数据
         $plaformId = $plaformChildId ? $plaformChildId : $plaformId;
        if($plaformId==0){
            $adminReg = getApiAdminReg($plaformId,'all');     
        }else{
            $adminReg = getApiAdminReg($plaformId,'child'); 
        } 
        try { 
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
            $AdminCompanyService = new AdminCompanyServiceClient(NULL);
            \ApiClient::build($AdminCompanyService);
            $CompanyDo = new CompanyDTO(); 
            
            $CompanyDo->offset = $offset;
            $CompanyDo->num = $num; 
           
            
            $where = " isdelete = 0 and admin_reg in ('{$adminReg}') ";
            # 模糊查询
            if($liekName){
                // $where .= " and name like '%".$liekName."%' ";
                $where .= " and (name like '%".$liekName."%' or id='".$liekName."' or remark like '%".$liekName."%') ";
            }
            $CompanyDo->where = $where; 
            $result = $AdminCompanyService->companyList($CompanyDo); 

            if($selectId){
                $model= Db::name('company');
                $filed = "id,name,status,admin_reg,remark";
                $map['id'] = ['in',$selectId];
                $map['isdelete'] = 0; 
                $companyList = Db::name('company')->where($map)->field($filed)->select();
                $plaformList = Db::name('plaform')->where(['isdelete'=>0])->field('id,reals,adminRealname,admin_reg')->select();
                for($i=0; $i<count($companyList); $i++ ){
                    $companyList[$i]['plaformname'] = "总平台";
                    for($j=0; $j<count($plaformList); $j++){
                        if($companyList[$i]['admin_reg']==$plaformList[$j]['admin_reg']){
                            $companyList[$i]['plaformname'] = $plaformList[$j]['reals'];
                        }
                    }
                }
            }else{
                $companyList =$result->data?$result->data : [];
            } 
            if($companyList){
                $header = ['ID','公司名称','所属平台','描述','状态'];
                $list = [];
                foreach($companyList as $v){
                    $list[] = [
                       $v['id'],
                        $v['name'],
                        $v['plaformname'],
                        $v['remark'],
                        $this->getStatsu($v['status']),
                    ];
                }
                $version = '2007';
                \Excel::export($header, $list, "公司列表", $version);
            }else{
                $msg['msg'] = "没有可导出的数据";    
            }

        } catch (Exception $ex) {
            $msg['msg'] = "没有可导出的数据";
        }
        return json_encode($msg);
    }

    #账号状态
    public function getStatsu($status=0){
        $status_str = "未知";
        if($status==0){
            $status_str = "禁用";
        }
        if($status==1){
            $status_str = "正常";
        }
        if($status==2){
            $status_str = "平台已禁用";
        }
        return $status_str;
    }
    /**
     * 导出部门
     */
    public function downExeclDepartment(){
        ob_end_clean(); # 清空缓冲区
        $msg = ['code'=>400, 'msg'=>'没有可导出的数据', 'data'=>[],'count'=>0];
        $plaformId = $this->request->param('plaformId') ? $this->request->param('plaformId') : 0; # 平台id
        $liekName = $this->request->param('name') ? $this->request->param('name') : ""; #部门名称
        $companyId = $this->request->param('companyid') ? $this->request->param('companyid') : ""; #公司id
        $plaformChildId = $this->request->param('plaformChildId') ? $this->request->param('plaformChildId') : 0; # 赛选平台
        $offset = $this->request->param('offset') ? $this->request->param('offset') : 0; # 分页
        $num = $this->request->param('num') ? $this->request->param('num') : 10000; # 条数

        # 选择id
        $selectId = $this->request->param("selectId") ? $this->request->param("selectId") : "";
        # 获取该平台下子平台和当前平台的数据
         $plaformId = $plaformChildId ? $plaformChildId : $plaformId;
        if($plaformId==0){
            $adminReg = getApiAdminReg($plaformId,'all');     
        }else{
            $adminReg = getApiAdminReg($plaformId,'child'); 
        } 
        try { 
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
            $AdminCompanyService = new AdminCompanyServiceClient(NULL);
            \ApiClient::build($AdminCompanyService);
            $CompanyDo = new CompanyDTO(); 
            
            $CompanyDo->offset = $offset;
            $CompanyDo->num = $num; 
          
            
            $where = " isdelete = 0 and pid = 0 and admin_reg in ('{$adminReg}') ";
            # 模糊查询
            if($liekName){
                // $where .= " and name like '%".$liekName."%' ";
                $where .= " and (name like '%".$liekName."%' or id='".$liekName."' or remark like '%".$liekName."%')";
            }
            # 公司id
            if($companyId){
                $where .= " and companyId ='{$companyId}'";
            }
            $CompanyDo->where = $where;  
            $result = $AdminCompanyService->departmentList($CompanyDo); 
            $plaformList = Db::name('plaform')->where(['isdelete'=>0])->field('id,reals,adminRealname,admin_reg')->select();
            $filed = "id,name,status,admin_reg";
            $map['isdelete'] = 0; 
            $getCompanyList = Db::name('company')->where($map)->field($filed)->select();

            if($selectId){
                $filed = "id,name,status,admin_reg,companyId,remark";
                $map['id'] = ['in',$selectId];
                $map['isdelete'] = 0;
                $companyList = Db::name('department')->where($map)->field($filed)->select();
            }else{
                $companyList = $result->data ? $result->data :"";
            }
            for($i=0; $i<count($companyList); $i++ ){
                $companyList[$i]['plaformname'] = "总平台";
                $companyList[$i]['companyname'] = "暂无公司";
                for($j=0; $j<count($plaformList); $j++){ 
                    if($companyList[$i]['admin_reg']==$plaformList[$j]['admin_reg']){
                        $companyList[$i]['plaformname'] = $plaformList[$j]['reals'];

                    }
                }
                for($k=0; $k<count($getCompanyList); $k++){
                    if($getCompanyList[$k]['id']==$companyList[$i]['companyId']){
                        $companyList[$i]['companyname'] = $getCompanyList[$k]['name'];
                    }
                }
            }  
            $header = ["ID","部门名称","所属公司","所属平台", "描述","状态"];
            if($companyList){
                $list = [];
                foreach($companyList as $v){
                    $list[] = [
                       $v['id'],
                        $v['name'],
                        $v['companyname'],
                        $v['plaformname'],
                        $v['remark'],
                        $this->getStatsu($v['status']),
                    ];
                } 
                $version = '2007';
                \Excel::export($header, $list, "部门列表", $version); 
            }else{
                $msg['msg'] = "没有可导出的数据";
            }
                

        } catch (Exception $ex) {
            SLogger::getLogger('pub_thrift_service', 'thrift_error')->error('thrift call error', $ex);
        }
        return json_encode($msg);
    }
    /**
     * 导出班组
     */
    public function downGroupExecl(){
        ob_end_clean(); # 清空缓冲区
        $msg = ['code'=>400, 'msg'=>'没有可导出的数据', 'data'=>[],'count'=>0];
        $plaformId = $this->request->param('plaformId') ? $this->request->param('plaformId') : 0; # 平台id
        $liekName = $this->request->param('name') ? $this->request->param('name') : ""; #部门名称
        $companyId = $this->request->param('companyid') ? $this->request->param('companyid') : ""; #公司id
        $departmentId = $this->request->param('departmentid') ? $this->request->param('departmentid') : ""; #部门id
        $plaformChildId = $this->request->param('plaformChildId') ? $this->request->param('plaformChildId') : 0; # 赛选平台
        $offset = $this->request->param('offset') ? $this->request->param('offset') : 0; # 分页
        $num = $this->request->param('num') ? $this->request->param('num') : 10000; # 条数

        $selectId = $this->request->param("selectId") ? $this->request->param("selectId") : "";
       # 获取该平台下子平台和当前平台的数据
         $plaformId = $plaformChildId ? $plaformChildId : $plaformId;
        if($plaformId==0){
            $adminReg = getApiAdminReg($plaformId,'all');     
        }else{
            $adminReg = getApiAdminReg($plaformId,'child'); 
        } 
        
        try { 
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
            $AdminCompanyService = new AdminCompanyServiceClient(NULL);
            \ApiClient::build($AdminCompanyService);
            $CompanyDo = new CompanyDTO(); 
            
            $CompanyDo->offset = $offset;
            $CompanyDo->num = $num; 
           
            $where = " isdelete = 0 and pid <> 0 and admin_reg in ('{$adminReg}') ";
            # 模糊查询
            if($liekName){
                // $where .= " and name like '%".$liekName."%' ";
                $where .= " and (name like '%".$liekName."%' or id='".$liekName."' or remark like '%".$liekName."%')";
            }
            # 公司id
            if($companyId){
                $where .= " and companyId ='{$companyId}'";
            }
            # 部门id
            if($departmentId){
                $where .= " and pid='{$departmentId}' ";
            }
            $CompanyDo->where = $where;  
            $result = $AdminCompanyService->groupList($CompanyDo); 
            $plaformList = Db::name('plaform')->where(['isdelete'=>0])->field('id,reals,adminRealname,admin_reg')->select();
            $filed = "id,name,status,admin_reg";
            $map['isdelete'] = 0; 
            $getCompanyList = Db::name('company')->where($map)->field($filed)->select();
            $maps['isdelete'] = 0;
            $maps['pid'] = 0;
            $departmentlist = Db::name('department')->where($maps)->field($filed)->select();

            if($selectId){
                $filed = "id,name,status,companyId,pid,remark";
                $map['id'] = ['in',$selectId];
                $map['isdelete'] = 0;
                $companyList = Db::name('department')->where($map)->field($filed)->select();
            }else{
                $companyList = $result->data ? $result->data :"";
            }
            for($i=0; $i<count($companyList); $i++ ){
                $companyList[$i]['plaformname'] = "总平台";
                $companyList[$i]['companyname'] = "暂无公司";
                $companyList[$i]['departmentName'] = "暂无部门";
                for($j=0; $j<count($plaformList); $j++){ 
                    if($companyList[$i]['admin_reg']==$plaformList[$j]['admin_reg']){
                        $companyList[$i]['plaformname'] = $plaformList[$j]['reals'];

                    }
                }
                for($k=0; $k<count($getCompanyList); $k++){
                    if($getCompanyList[$k]['id']==$companyList[$i]['companyId']){
                        $companyList[$i]['companyname'] = $getCompanyList[$k]['name'];
                    }
                }
                for($y=0; $y<count($departmentlist);$y++){
                    if($departmentlist[$y]['id']==$companyList[$i]['pid']){
                        $companyList[$i]['departmentName'] = $departmentlist[$y]['name'];
                    }
                }
            } 
             $header = ["ID","班组名称", "所属平台","所属公司","所属部门","描述","状态"];
            if($companyList){
                $list = [];
                foreach($companyList as $v){
                    $list[] = [
                       $v['id'],
                        $v['name'],
                        $v['plaformname'],
                        $v['companyname'],
                        $v['departmentName'],
                        $v['remark'],
                        $this->getStatsu($v['status']),
                    ];
                } 
                $version = '2007';
                \Excel::export($header, $list, "班组列表", $version); 
            }else{
                $msg['msg'] = "没有可导出的数据";
            }
        } catch (Exception $ex) {
            $msg['msg'] = "没有可导出的数据";
        }
        return json_encode($msg);
    }
    /**
     *职位和专业 
     */
    public function downAllExecl(){
        ob_end_clean(); # 清空缓冲区
        $msg = ['code'=>400, 'msg'=>'暂无数据', 'data'=>[],'count'=>0];
        $plaformId = $this->request->param('plaformId') ? $this->request->param('plaformId') : 0; # 平台id
        $liekName = $this->request->param('name') ? $this->request->param('name') : ""; #专业名称
        $plaformChildId = $this->request->param('plaformChildId') ? $this->request->param('plaformChildId') : 0; # 赛选平台
        $type = $this->request->param('type') ? $this->request->param('type') : 0; # 0 专业 1职位
        $companyId = $this->request->param('companyId') ? $this->request->param('companyId') : 0; # 赛选公司
        $offset = $this->request->param('offset') ? $this->request->param('offset') : 0; # 分页
        $num = $this->request->param('num') ? $this->request->param('num') : 10000; # 条数

        $selectId =$this->request->param('selectId') ? $this->request->param('selectId') : ""; # 条数
        # 获取该平台下子平台和当前平台的数据
         $plaformId = $plaformChildId ? $plaformChildId : $plaformId;
        if($plaformId==0){
            $adminReg = getApiAdminReg($plaformId,'all');     
        }else{
            $adminReg = getApiAdminReg($plaformId,'child'); 
        } 
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
            $AdminCompanyService = new AdminCompanyServiceClient(NULL);
            \ApiClient::build($AdminCompanyService);
            $CompanyDo = new CompanyDTO(); 
            
            $CompanyDo->offset = $offset;
            $CompanyDo->num = $num; 
            $where = " isdelete = 0 and type='{$type}' and admin_reg in ('{$adminReg}') ";
            # 模糊查询
            if($liekName){
                // $where .= " and name like '%".$liekName."%' ";
                $where .= " and (name like '%".$liekName."%' or id='".$liekName."' or remark like '%".$liekName."%')";
            }  
            # 查询属于哪个公司
            if($companyId){
                $where .= " and companyId = ".$companyId;
            }
            $CompanyDo->where = $where;  
            $result = $AdminCompanyService->specialtyList($CompanyDo); 
              if($selectId){
                $filed = "id,name,status,remark";
                $map['id'] = ['in',$selectId];
                $map['isdelete'] = 0;
                $companyList = Db::name('user_specialty')->where($map)->field($filed)->select();
            }else{
                $companyList = $result->data ? $result->data :"";
            }
            $plaformList = Db::name('plaform')->where(['isdelete'=>0])->field('id,reals,adminRealname,admin_reg')->select();
            $filed = "id,name,status,admin_reg";
            $maps['isdelete'] = 0; 
            $getCompanyList = Db::name('company')->where($maps)->field($filed)->select();

             for($i=0; $i<count($companyList); $i++ ){
                $companyList[$i]['plaformname'] = "总平台";
                $companyList[$i]['companyname'] = "暂无公司"; 
                for($j=0; $j<count($plaformList); $j++){ 
                    if($companyList[$i]['admin_reg']==$plaformList[$j]['admin_reg']){
                        $companyList[$i]['plaformname'] = $plaformList[$j]['reals'];

                    }
                }
                for($k=0; $k<count($getCompanyList); $k++){
                    if($getCompanyList[$k]['id']==$companyList[$i]['companyId']){
                        $companyList[$i]['companyname'] = $getCompanyList[$k]['name'];
                    }
                } 
            }
             $header = ["ID","名称","所属公司","所属平台", "描述","状态"];
            if($companyList){
                $list = [];
                foreach($companyList as $v){
                    $list[] = [
                       $v['id'],
                        $v['name'],
                        $v['companyname'],
                        $v['plaformname'],
                        $v['remark'],
                        $this->getStatsu($v['status']),
                    ];
                }
                $version = '2007'; 
                if($type==1){
                    $name = "职位列表";
                }else{
                    $name = "专业列表";
                }
                \Excel::export($header, $list, $name, $version); 
            }
        
        return json_encode($msg);
    }

    /**
     * 修改部门状态 
     * status 0：禁用 1:启用  2：平台禁用
     */
    public function editDepartStatus(){
        $msg = ['code'=>400, 'msg'=>"修改失败", 'data'=>[]];
        $plaformId = $this->request->param('plaformId') ? $this->request->param('plaformId') : 0;
        $admin_reg = $this->request->param('reg') ? $this->request->param('reg') : "";
        $departmentId = $this->request->param('departmentId') ? $this->request->param('departmentId') : "";
        $status = $this->request->param('status') ? $this->request->param('status') : "0";
       
        if(empty($departmentId)){
            $msg['msg'] = "缺少必要参数";
            return json_encode($msg);
        } 
        try{ 
            $admin = Db::name("admin_user")->where(['id'=>1])->field('reg')->find();
            $where['id'] = ['in',$departmentId];
            if($plaformId==0){ 
                    if($status==0){ # 禁用
                        $upstatus = Db::name('department')->where($where)->setField(['status'=>2]);
                    }else{ # 启用
                        $upstatus = Db::name('department')->where($where)->setField(['status'=>1]);
                    } 

            # 子平台修改状态
            }else{
                $where['id'] = ['in',$departmentId];
                $upstatus = Db::name('department')->where($where)->setField(['status'=>$status]);
            }
            if($upstatus){
                $msg['code'] = 200;
                $msg['msg'] = "禁用成功";
                if($status==1){
                    $msg['msg'] = "启用成功";
                }
                
            }
        }catch(Exception $e){
            $msg['msg'] = $e->getMessage();
        }
        return json_encode($msg);
    }
   

    /**
     * 修改专业和职位
     * status 0：禁用 1:启用  2：平台禁用
     */
    public function editSpecStatus(){
        $msg = ['code'=>400, 'msg'=>"修改失败", 'data'=>[]];
        $plaformId = $this->request->param('plaformId') ? $this->request->param('plaformId') : 0;
        $admin_reg = $this->request->param('reg') ? $this->request->param('reg') : "";
        $id = $this->request->param('id') ? $this->request->param('id') : "";
        $status = $this->request->param('status') ? $this->request->param('status') : "0";
       
        if(empty($id)){
            $msg['msg'] = "缺少必要参数";
            return json_encode($msg);
        } 
        try{ 
            $admin = Db::name("admin_user")->where(['id'=>1])->field('reg')->find();
            $where['id'] = ['in',$id];
            if($plaformId==0){ 
                if($status==0){ # 禁用
                    $upstatus = Db::name('user_specialty')->where($where)->setField(['status'=>2]);
                }else{ # 启用
                    $upstatus = Db::name('user_specialty')->where($where)->setField(['status'=>1]);
                } 
            # 子平台修改状态
            }else{
                $where['id'] = ['in',$id];
                $upstatus = Db::name('user_specialty')->where($where)->setField(['status'=>$status]);
            }
            if($upstatus){
                $msg['code'] = 200;
                $msg['msg'] = "禁用成功";
                if($status==1){
                    $msg['msg'] = "启用成功";
                }
                
            }
        }catch(Exception $e){
            $msg['msg'] = $e->getMessage();
        }
        return json_encode($msg);
    }
     /**
     * 删除部门 
     */
    public function deleteSpec(){
        $msg = ['code'=>400, 'msg'=>"删除失败", 'data'=>[]];
        $plaformId = $this->request->param('plaformId') ? $this->request->param('plaformId') : 0;
        $admin_reg = $this->request->param('reg') ? $this->request->param('reg') : "";
        $id = $this->request->param('id') ? $this->request->param('id') : "";
        $delete = $this->request->param('delete') ? $this->request->param('delete') : "1";
       
        if(empty($id) || empty($delete)){
            $msg['msg'] = "缺少必要参数";
            return json_encode($msg);
        } 
        try{ 
            $where['id'] = ['in',$id];
            $upstatus = Db::name('user_specialty')->where($where)->setField(['isdelete'=>$delete]); 
            if($upstatus){
                $msg['code'] = 200;
                $msg['msg'] = "已删除";
            }
        }catch(Exception $e){
            $msg['msg'] = $e->getMessage();
        }
        return json_encode($msg);
    }
     /**
     * 删除班组或部门 
     */
    public function deleteDepart(){
        $msg = ['code'=>400, 'msg'=>"删除失败", 'data'=>[]];
        $plaformId = $this->request->param('plaformId') ? $this->request->param('plaformId') : 0;
        $admin_reg = $this->request->param('reg') ? $this->request->param('reg') : "";
        $departmentId = $this->request->param('departmentId') ? $this->request->param('departmentId') : "";
        $delete = $this->request->param('delete') ? $this->request->param('delete') : "1";
       
        if(empty($departmentId) || empty($delete)){
            $msg['msg'] = "缺少必要参数";
            return json_encode($msg);
        } 
        try{ 
            $where['id'] = ['in',$departmentId];
            $upstatus = Db::name('department')->where($where)->setField(['isdelete'=>$delete]); 
            if($upstatus){
                $msg['code'] = 200;
                $msg['msg'] = "已删除";
            }
        }catch(Exception $e){
            $msg['msg'] = $e->getMessage();
        }
        return json_encode($msg);
    }
}
